package model.commands.commandable;

import java.util.List;
import model.Environment;
import model.SyntaxErrorException;
import model.commands.Command;
import model.turtle.Commandable;


/**
 * Name:
 * TOWARDS x y
 * 
 * Description:
 * turns turtle to face the point (x, y), where (0, 0) is the center of the screen
 * returns the number of degrees turtle turned
 * 
 * @author JLongley
 * @author Erick Gonzalez
 */
public class TowardsCommand extends Command {
    private static final double DEGREES = 180.0;

    @Override
    public int evaluate (Environment e) throws SyntaxErrorException {
        // calculate the angle between the current position and the towards
        // position
        List<Command> myArguments = getArguments();

        int x = myArguments.get(0).evaluate(e);
        int y = myArguments.get(1).evaluate(e);

        Commandable c = e.invokeCommandableMethod(Environment.SET_TOWARDS, x, y);

        double deltaX = x - c.getXCoordinate();
        double deltaY = y - c.getYCoordinate();
        int oldHeading = c.getHeading();

        double newHeading = Math.atan(deltaY / deltaX) * DEGREES / Math.PI;

        // return angle turned
        return (int) (newHeading - oldHeading);
    }

}
